package top.mcwebsite.playandroid.business.me.impl.main.ui

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Divider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import androidx.paging.LoadState
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.compose.collectAsLazyPagingItems
import top.mcwebsite.playandroid.business.me.impl.model.CollectionArticleModel
import top.mcwebsite.playandroid.business.me.impl.main.vm.CollectionArticleIntent
import top.mcwebsite.playandroid.business.me.impl.main.vm.CollectionArticleUIEffect
import top.mcwebsite.playandroid.business.me.impl.main.vm.CollectionArticleViewModel
import top.mcwebsite.playandroid.lib.common.ext.encodeURL

@Composable
fun CollectionArticle(
    navController: NavController
) {
    val viewModel: CollectionArticleViewModel = viewModel()
    viewModel.HandleUiEffect {
        when (it) {
            is CollectionArticleUIEffect.GoArticleDetailUIEffect -> {
                navController.navigate("article_detail/${it.link.encodeURL()}")
            }
        }
    }

    val lazyPagingItems = viewModel.pagerFlow.collectAsLazyPagingItems()
    val lazyColumnState = rememberLazyListState()
    LazyColumn(
        state = lazyColumnState,
        modifier = Modifier.padding(top = 10.dp)
    ) {
        items(count = lazyPagingItems.itemCount) { index ->
            val item = lazyPagingItems[index]
            if (item != null) {
                CollectionArticleItem(collectionArticleModel = item) {
                    viewModel.sendIntent(CollectionArticleIntent.ClickArticleIntent(it))
                }
                if (index < lazyPagingItems.itemCount - 1) {
                    Divider(Modifier.padding(vertical = 10.dp))
                }
            }
        }

        // Add a loading item at the end
        if (lazyPagingItems.loadState.append == LoadState.Loading) {
            item {
                CircularProgressIndicator(
                    modifier = Modifier
                        .width(50.dp)
                        .padding(16.dp)
                )
            }
        }

        // Handle errors
        if (lazyPagingItems.loadState.append is LoadState.Error) {
            val errorState = lazyPagingItems.loadState.append as LoadState.Error
            item {
                Text(
                    text = "Error: ${errorState.error.localizedMessage}",
                    modifier = Modifier.padding(16.dp)
                )
            }
        }
        if (lazyPagingItems.loadState.append is LoadState.NotLoading) {
            item {
                Spacer(Modifier.height(20.dp))
            }
        }
    }
}

@Composable
private fun CollectionArticleItem(
    collectionArticleModel: CollectionArticleModel,
    onClickItem: (CollectionArticleModel) -> Unit
) {
    Column(
        modifier = Modifier
            .padding(vertical = 5.dp, horizontal = 10.dp)
            .clickable {
                onClickItem.invoke(collectionArticleModel)
            }
    ) {
        Text(text = collectionArticleModel.title, style = MaterialTheme.typography.titleMedium)
        Text(
            text = "作者: ${collectionArticleModel.getDisplayAuthor()} 分类: ${collectionArticleModel.chapterName}",
            style = MaterialTheme.typography.labelMedium
        )
        Text("收藏时间: " + collectionArticleModel.niceDate, style = MaterialTheme.typography.labelMedium)
    }
}